package com.sam.lazyload;

public class InstanceFactory {

	/**运用静态内部类实现单例**/
	private static class InstanceHolder {
		private static  InstanceFactory instance = new InstanceFactory();
	}

	public static InstanceFactory getInstance() {
		return InstanceHolder.instance; // 这里将导致InstanceHolder类被初始化
	}

	public static void main(String[] args) {

		for (int i = 0; i < 100; i++) {

			new Thread(new Runnable() {

				@Override
				public void run() {

					System.out.println(Thread.currentThread().getName()
							+ "获取实例："
							+ InstanceFactory.getInstance().toString());

				}
			}).start();
		}

		System.out.println(Thread.currentThread().getName() + "获取实例："
				+ InstanceFactory.getInstance().toString());

	}
}
